<?php
require_once 'class/db/Database.php';

define ('DB_TXT_FASE_LEN',              20);
define ('DB_TXT_FASE_COLOR_LEN',        6);
/**
 * Description of DbFase
 *
 * @author GHoogendoorn
 * @version 0.1
 * 
 * Version history
 * 0.1  GHoogendoorn    Initial version
 */
class DbFase extends Database{
    
    private $id;
    private $fase_color;
    private $fase_order;
    private $fase;

    public function __construct($id='') {
        parent::__construct();
        
        //Initialisatie
        if (!empty($id)){
            $this->selectById($id);
        }
    }
    
    public function delete(){
        try{
            $this->dbCheckId($this->id, 'fase_id');
            
            $query = "DELETE   FROM `fase` where `fase_id` = '" .$this->id."'";
            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';
            var_dump($query);
            echo '</pre>';
            //*/
            
            $this->query($query);
            
            $this->reset();
            
            return true;
            
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
    }
    
    public function getId() {
        return $this->id;
    }

    public function getFase_color() {
        return $this->fase_color;
    }

    public function setFase_color($fase_color) {
        $this->fase_color = $fase_color;
    }

    public function getFase() {
        return $this->fase;
    }

    public function setFase($fase) {
        $this->fase = $fase;
    }
    
    public function getOrder() {
        return $this->fase_order;
    }

    public function setOrder($fase_order) {
        $this->fase_order = $fase_order;
    }

        
    public function getFaseIds(){
        
        /**
         * Check input fields
         */
        try{
            
            $query = "SELECT `fase_id` FROM `fase` ORDER BY `order`";
            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';
            var_dump($query);
            echo '</pre>';
            //*/
            
            $this->query($query);
            
            /**
             * Retrieve array as:
             *  array( 0 => array(fase_id => 1 ))
             */
            $tmp_result = $this->dbFetchAll();
            
            /**
             * Change result to
             *  array( 0 => 1 )
             */
            $result_array = array();
            foreach( $tmp_result as $idx => $fase_array){
                $result_array[$idx] = (int)$fase_array['fase_id'];
            }
            
            return $result_array;
            
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
        
    }

    /**
     * Save the given fields<br />
     * Than store the fields in the class attributes.<br />
     * <br/>
     * trigger_error is called when an error is found.
     * 
     * @param String $fase Description of this fase
     * @param String $fase_color The hexcode of the color e.g. 'ff00ff'<br/>
     *                           Gives the possibility to color different fases.
     */
    public function save($fase, $fase_color, $fase_order){
        /**
         * Check input fields
         */
        try{
            $this->checkText($fase, DB_TXT_FASE_LEN);
            $this->checkText($fase_color, DB_TXT_FASE_COLOR_LEN);
            $this->checkNumber($fase_order, 'fase_order');
            
            $query = 'INSERT INTO `project_status`.`fase` (`fase_id`, `fase`, `fase_kleur`, `order`)'.
                     " VALUES (NULL, '".$fase."', '".$fase_color."', '".$fase_order."')";
            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';
            var_dump($query);
            echo '</pre>';
            //*/
            
            $this->query($query);
            /** 
             * Retrieve generated ID
             * And fill class attributes
             */        
            $this->selectById($this->dbGetInsertedId() );
            
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
        
        
    }
    
    private function reset(){
        $this->id = '';
        $this->fase = '';
        $this->fase_color = '';
        $this->fase_order = '';
        
        return true;
    }
    
    /**
     *  The class attributes are set after database select for this id.
     *  trigger_error() is called when an error occurs.
     * 
     * @param int $id Id of the retrieved row set.
     */
    private function selectById($id){
        
        try{
            $this->checkId($id, 'fase_id');
            
            $query = 'SELECT `fase`, `fase_kleur`,`order`  FROM `fase` where `fase_id` = '.$id;

            $this->query($query);
            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';
            var_dump($query);
            echo '</pre>';
            //*/
            
            $result = $this->fetchArray();
            
            $this->fase = $result['fase'];
            $this->fase_color = $result['fase_kleur'];
            $this->fase_order = $result['order'];
            $this->id = $id;
            
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
    }
}

class TestDbFase {

    public function TestDbFase() {
        try {
            $test = new dbFase();
            $test->save('Test fase', '1010FF', 7);
            $return = $test->delete();
            var_dump($test);
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            
            trigger_error($e->getMessage());
            echo '</pre>';
        }
    }

}

?>
